<template>
  <div class="inventory-forecast">
    <!-- 顶部工具栏 -->
    <div class="toolbar">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/forecast' }">智能预测</el-breadcrumb-item>
        <el-breadcrumb-item>库存预测</el-breadcrumb-item>
      </el-breadcrumb>
      <div class="toolbar-actions">
        <el-select v-model="selectedWarehouse" placeholder="选择仓库" style="width: 150px; margin-right: 10px">
          <el-option label="全部仓库" value="all" />
          <el-option label="中心仓" value="center" />
          <el-option label="华东仓" value="east" />
          <el-option label="华南仓" value="south" />
        </el-select>
        <el-button type="primary" @click="handleForecast">
          <el-icon><MagicStick /></el-icon>
          开始预测
        </el-button>
        <el-button @click="handleExport">
          <el-icon><Download /></el-icon>
          导出报告
        </el-button>
      </div>
    </div>

    <!-- 库存预警卡片 -->
    <el-row :gutter="20" class="alert-cards">
      <el-col :span="6">
        <el-card shadow="hover" class="alert-card danger">
          <div class="alert-content">
            <div class="alert-icon">
              <el-icon :size="40"><WarningFilled /></el-icon>
            </div>
            <div class="alert-info">
              <div class="alert-value">23</div>
              <div class="alert-label">预警商品</div>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover" class="alert-card warning">
          <div class="alert-content">
            <div class="alert-icon">
              <el-icon :size="40"><Warning /></el-icon>
            </div>
            <div class="alert-info">
              <div class="alert-value">45</div>
              <div class="alert-label">需补货商品</div>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover" class="alert-card success">
          <div class="alert-content">
            <div class="alert-icon">
              <el-icon :size="40"><CircleCheck /></el-icon>
            </div>
            <div class="alert-info">
              <div class="alert-value">156</div>
              <div class="alert-label">库存正常</div>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover" class="alert-card info">
          <div class="alert-content">
            <div class="alert-icon">
              <el-icon :size="40"><Box /></el-icon>
            </div>
            <div class="alert-info">
              <div class="alert-value">12</div>
              <div class="alert-label">积压商品</div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 库存趋势预测 -->
    <el-row :gutter="20">
      <el-col :span="16">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span><el-icon><TrendCharts /></el-icon> 库存趋势预测</span>
              <el-button-group size="small">
                <el-button>7天</el-button>
                <el-button type="primary">15天</el-button>
                <el-button>30天</el-button>
              </el-button-group>
            </div>
          </template>
          <div ref="trendChartRef" style="height: 400px"></div>
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card shadow="hover">
          <template #header>
            <span><el-icon><PieChart /></el-icon> 库存健康度</span>
          </template>
          <div ref="healthChartRef" style="height: 400px"></div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 补货建议 -->
    <el-card shadow="hover" style="margin-top: 20px">
      <template #header>
        <div class="card-header">
          <span><el-icon><ShoppingCart /></el-icon> 智能补货建议</span>
          <el-input
            v-model="searchKeyword"
            placeholder="搜索商品"
            style="width: 200px"
            clearable
          >
            <template #prefix>
              <el-icon><Search /></el-icon>
            </template>
          </el-input>
        </div>
      </template>
      <el-table :data="replenishData" stripe style="width: 100%">
        <el-table-column type="index" label="#" width="60" />
        <el-table-column prop="productName" label="商品名称" min-width="200">
          <template #default="{ row }">
            <div class="product-info">
              <el-avatar :size="40" :src="row.image" shape="square" />
              <span>{{ row.productName }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="currentStock" label="当前库存" width="100" />
        <el-table-column prop="forecastDemand" label="预测需求" width="100">
          <template #default="{ row }">
            <span style="color: #409eff; font-weight: bold">{{ row.forecastDemand }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="recommendStock" label="建议补货" width="100">
          <template #default="{ row }">
            <span style="color: #67c23a; font-weight: bold">{{ row.recommendStock }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="priority" label="优先级" width="100">
          <template #default="{ row }">
            <el-tag :type="getPriorityType(row.priority)">{{ row.priority }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="stockoutDays" label="预计缺货天数" width="120">
          <template #default="{ row }">
            <span :class="row.stockoutDays <= 3 ? 'danger-text' : ''">{{ row.stockoutDays }}天</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-button link type="primary" @click="handleCreatePurchase(row)">
              <el-icon><Plus /></el-icon>创建采购
            </el-button>
            <el-button link type="success" @click="handleViewDetail(row)">
              <el-icon><View /></el-icon>详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 库存周转分析 -->
    <el-row :gutter="20" style="margin-top: 20px">
      <el-col :span="12">
        <el-card shadow="hover">
          <template #header>
            <span><el-icon><DataAnalysis /></el-icon> 库存周转率预测</span>
          </template>
          <div ref="turnoverChartRef" style="height: 300px"></div>
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card shadow="hover">
          <template #header>
            <span><el-icon><Money /></el-icon> 库存成本预测</span>
          </template>
          <div ref="costChartRef" style="height: 300px"></div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, onMounted, nextTick } from 'vue'
import { ElMessage } from 'element-plus'
import * as echarts from 'echarts'

const selectedWarehouse = ref('all')
const searchKeyword = ref('')

// 补货数据
const replenishData = ref([
  {
    id: 1,
    productName: 'iPhone 15 Pro Max 256GB',
    image: 'https://via.placeholder.com/40',
    currentStock: 45,
    forecastDemand: 180,
    recommendStock: 135,
    priority: '紧急',
    stockoutDays: 2
  },
  {
    id: 2,
    productName: '华为Mate 60 Pro',
    image: 'https://via.placeholder.com/40',
    currentStock: 68,
    forecastDemand: 210,
    recommendStock: 142,
    priority: '高',
    stockoutDays: 3
  },
  {
    id: 3,
    productName: '小米14 Ultra',
    image: 'https://via.placeholder.com/40',
    currentStock: 92,
    forecastDemand: 256,
    recommendStock: 164,
    priority: '高',
    stockoutDays: 4
  },
  {
    id: 4,
    productName: 'MacBook Pro M3',
    image: 'https://via.placeholder.com/40',
    currentStock: 125,
    forecastDemand: 198,
    recommendStock: 73,
    priority: '中',
    stockoutDays: 7
  },
  {
    id: 5,
    productName: 'iPad Air 2024',
    image: 'https://via.placeholder.com/40',
    currentStock: 156,
    forecastDemand: 234,
    recommendStock: 78,
    priority: '中',
    stockoutDays: 8
  }
])

// 图表引用
const trendChartRef = ref(null)
const healthChartRef = ref(null)
const turnoverChartRef = ref(null)
const costChartRef = ref(null)

// 开始预测
const handleForecast = () => {
  ElMessage.success('正在执行库存预测分析...')
}

// 导出报告
const handleExport = () => {
  ElMessage.success('报告导出成功')
}

// 创建采购单
const handleCreatePurchase = (row) => {
  ElMessage.success(`正在为"${row.productName}"创建采购单...`)
}

// 查看详情
const handleViewDetail = (row) => {
  ElMessage.info(`查看商品详情：${row.productName}`)
}

// 获取优先级类型
const getPriorityType = (priority) => {
  const typeMap = {
    '紧急': 'danger',
    '高': 'warning',
    '中': 'primary',
    '低': 'info'
  }
  return typeMap[priority] || ''
}

// 初始化图表
const initCharts = () => {
  // 库存趋势图表
  if (trendChartRef.value) {
    const trendChart = echarts.init(trendChartRef.value)
    const option = {
      tooltip: {
        trigger: 'axis'
      },
      legend: {
        data: ['实际库存', '预测库存', '安全库存', '最高库存']
      },
      grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
      },
      xAxis: {
        type: 'category',
        data: ['第1天', '第3天', '第5天', '第7天', '第9天', '第11天', '第13天', '第15天']
      },
      yAxis: {
        type: 'value',
        name: '库存量'
      },
      series: [
        {
          name: '实际库存',
          type: 'line',
          data: [1250, 1180, 1150, 1080, 1020, null, null, null],
          itemStyle: { color: '#409eff' }
        },
        {
          name: '预测库存',
          type: 'line',
          data: [null, null, null, null, 1020, 950, 880, 820],
          itemStyle: { color: '#67c23a' },
          lineStyle: { type: 'dashed' }
        },
        {
          name: '安全库存',
          type: 'line',
          data: [800, 800, 800, 800, 800, 800, 800, 800],
          itemStyle: { color: '#e6a23c' },
          lineStyle: { type: 'dotted' }
        },
        {
          name: '最高库存',
          type: 'line',
          data: [1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500],
          itemStyle: { color: '#f56c6c' },
          lineStyle: { type: 'dotted' }
        }
      ]
    }
    trendChart.setOption(option)
  }

  // 库存健康度图表
  if (healthChartRef.value) {
    const healthChart = echarts.init(healthChartRef.value)
    const option = {
      tooltip: {
        trigger: 'item'
      },
      legend: {
        orient: 'vertical',
        left: 'left'
      },
      series: [
        {
          name: '库存状态',
          type: 'pie',
          radius: '50%',
          data: [
            { value: 156, name: '正常', itemStyle: { color: '#67c23a' } },
            { value: 45, name: '需补货', itemStyle: { color: '#e6a23c' } },
            { value: 23, name: '预警', itemStyle: { color: '#f56c6c' } },
            { value: 12, name: '积压', itemStyle: { color: '#909399' } }
          ],
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        }
      ]
    }
    healthChart.setOption(option)
  }

  // 库存周转率图表
  if (turnoverChartRef.value) {
    const turnoverChart = echarts.init(turnoverChartRef.value)
    const option = {
      tooltip: {
        trigger: 'axis'
      },
      legend: {
        data: ['实际周转率', '预测周转率']
      },
      xAxis: {
        type: 'category',
        data: ['1月', '2月', '3月', '4月', '5月', '6月']
      },
      yAxis: {
        type: 'value',
        name: '周转率'
      },
      series: [
        {
          name: '实际周转率',
          type: 'bar',
          data: [4.2, 4.5, 4.8, 5.1, null, null],
          itemStyle: { color: '#409eff' }
        },
        {
          name: '预测周转率',
          type: 'bar',
          data: [null, null, null, null, 5.3, 5.5],
          itemStyle: { color: '#67c23a' }
        }
      ]
    }
    turnoverChart.setOption(option)
  }

  // 库存成本图表
  if (costChartRef.value) {
    const costChart = echarts.init(costChartRef.value)
    const option = {
      tooltip: {
        trigger: 'axis'
      },
      legend: {
        data: ['实际成本', '预测成本']
      },
      xAxis: {
        type: 'category',
        data: ['1月', '2月', '3月', '4月', '5月', '6月']
      },
      yAxis: {
        type: 'value',
        name: '成本(万元)'
      },
      series: [
        {
          name: '实际成本',
          type: 'line',
          data: [85, 92, 88, 95, null, null],
          itemStyle: { color: '#409eff' },
          areaStyle: { opacity: 0.3 }
        },
        {
          name: '预测成本',
          type: 'line',
          data: [null, null, null, 95, 98, 102],
          itemStyle: { color: '#67c23a' },
          lineStyle: { type: 'dashed' },
          areaStyle: { opacity: 0.3 }
        }
      ]
    }
    costChart.setOption(option)
  }
}

onMounted(() => {
  nextTick(() => {
    initCharts()
  })
})
</script>

<style scoped lang="scss">
.inventory-forecast {
  padding: 20px;
  background: #f5f7fa;
  min-height: calc(100vh - 60px);
}

.toolbar {
  background: white;
  padding: 15px 20px;
  border-radius: 8px;
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .toolbar-actions {
    display: flex;
    align-items: center;
  }
}

.alert-cards {
  margin-bottom: 20px;

  .alert-card {
    border-radius: 12px;

    &.danger {
      border-left: 4px solid #f56c6c;
    }

    &.warning {
      border-left: 4px solid #e6a23c;
    }

    &.success {
      border-left: 4px solid #67c23a;
    }

    &.info {
      border-left: 4px solid #909399;
    }

    .alert-content {
      display: flex;
      gap: 15px;
      align-items: center;

      .alert-icon {
        width: 60px;
        height: 60px;
        border-radius: 12px;
        display: flex;
        align-items: center;
        justify-content: center;
      }

      .alert-info {
        flex: 1;

        .alert-value {
          font-size: 28px;
          font-weight: bold;
          color: #303133;
          margin-bottom: 5px;
        }

        .alert-label {
          font-size: 14px;
          color: #909399;
        }
      }
    }
  }
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;

  span {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: bold;
  }
}

.product-info {
  display: flex;
  align-items: center;
  gap: 10px;
}

.danger-text {
  color: #f56c6c;
  font-weight: bold;
}
</style>

